//
//  JuTableViewController.m
//  TestPresen
//
//  Created by Juvid on 16/9/1.
//  Copyright © 2016年 Juvid. All rights reserved.
//

#import "JuTableViewController.h"
#import "UIScrollView+JuRefresh.h"
#import "HeadTableViewController.h"
@interface JuTableViewController (){
    JuRefreshHead  *reshenHead;
    JuRefreshFoot *reshenFoot;
}

@end

@implementation JuTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.edgesForExtendedLayout=UIRectEdgeNone;

    reshenHead=[JuRefreshHead juHeadWithhandle:^{
        NSLog(@"开始刷新");
    }];
    self.tableView.ju_RefreshHead=reshenHead;

    self.tableView.ju_RefreshFoot=[JuRefreshFoot juFoot];
//        self.title=@"专业下拉刷新";
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [reshenHead juStartRefresh];
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated{

}
#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 8;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
//    return 3;
//}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     [reshenFoot juLoadMoreStatus:JuLoadStatusSuccess];
//    NSLog(@"%f  %f ", scrollView.contentOffset.y, scrollView.contentInset.top);
//    [reshenHead shWillReshen:scrollView.contentOffset];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
   [self.tableView.ju_RefreshFoot juLoadMoreStatus:JuLoadStatusIng];
}
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [UIView animateWithDuration:0.5 animations:^{
//        scrollView.contentInset=UIEdgeInsetsMake(128, 0, 0, 0);
//        scrollView.contentOffset=CGPointMake(0, -128);
//    }completion:^(BOOL finished) {
//        [reshenHead shStartReshen];
//    }];
//
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HeadTableViewController *head=[[HeadTableViewController alloc]init];
    [self.navigationController pushViewController:head animated:YES];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

//    [reshenHead shStartReshen];
    //下拉刷新-当下拉刷新时，contentOffset实际上就是greenView相对于屏幕左上角的偏移量。
//    if (scrollView.contentOffset.y < -164) {
//
//        [UIView animateWithDuration:0.3 animations:^{
//
//            scrollView.contentInset = UIEdgeInsetsMake(128, 0, 0, 0);
//
//        } completion:^(BOOL finished) {
//
//            NSLog(@"发起下拉刷新");
//
//            //设置延时时间为2秒
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//                [UIView animateWithDuration:0.3 animations:^{
//                    //恢复之前的contentInset，让greenView回到原来的地方
//                    scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
//                }];
//            });
//
//        }];
//    }

    //上拉加载

//    if (scrollView.bounds.size.height +  scrollView.contentOffset.y >scrollView.contentSize.height) {
//
//        [UIView animateWithDuration:1.0 animations:^{
//
//            scrollView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
//
//        } completion:^(BOOL finished) {
//
//            NSLog(@"发起上拉加载");
//
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//                [UIView animateWithDuration:1.0 animations:^{
//
//                    scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
//
//                }];
//            });
//        }];
//
//        
//    }

}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
